package thread.threadgroup;

import java.util.concurrent.TimeUnit;

/**
 * 线程组基本操作
 *
 * @author lwmzzz
 * @version 1.0
 * @date 2021/5/16 19:37
 */
public class Test02 {
    public static void main(String[] args) {
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");

        Runnable runnable = () -> {
            while (true) {
                System.out.println("==============================当前线程： " + Thread.currentThread());

                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t1 = new Thread(runnable, "t1");
        Thread t2 = new Thread(threadGroup, runnable, "t2");

        t1.start();
        t2.start();


        System.out.println(mainGroup.activeCount());
        System.out.println(mainGroup.activeGroupCount());
        System.out.println(mainGroup.parentOf(threadGroup));
        System.out.println(mainGroup.parentOf(mainGroup));//自己是自己的父线程组
        System.out.println(mainGroup.getParent());
    }
}
